module.exports = class AsyncSeriesBailHook {
  constructor(args){
      this.tasks = []
  }
  tapAsync(name,task){
   this.tasks.push(task)
  }
  callAsync(...args){
   const finalCallback = args.pop()
   let index = 0;
   const next = (err,...others)=> {
    const task = this.tasks[index]
    if(!task) return finalCallback()
    if(index === 0){
      task(...args,next)
    }else{
      if(!err){
        task(others,next)
      }else{
        finalCallback()
      }
    }
    index++
   }

   next()
  }
  tapPromise(name,task){
   this.tasks.push(task)
  }
  promise(...args){
    const [first,...others] = this.tasks
    return others.reduce((p,n)=>{
      return p.then((err)=>{
        console.log('err',err,);
        
        if(!err){
          return n(args)
        }else{
          return Promise.resolve()
        }
        
      })
    },first(...args))
  }
}
